package nancal.mp.mvc.devops

import nancal.mp.db.mongo.JenkinsJobStatusEnum
import nancal.mp.db.mongo.entity.dev.JenkinsLog
import nancal.mp.db.mongo.mor
import nbcp.base.annotation.Require
import nbcp.base.comm.ListResult
import nbcp.base.extend.AsInt
import nbcp.myoql.db.mongo.query
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest


@RequestMapping("/dev/jenkins")
@RestController
class JenkinsLogController {
    @PostMapping("/tasks")
    fun task_list(
        @Require type: JenkinsJobStatusEnum,
        @Require take: Int,
        request: HttpServletRequest
    ): ListResult<JenkinsLog> {
        return mor.dev.jenkinsLog.query()
            .where { it.status mongoEquals type }
            .limit(0, Math.max(take.AsInt(12), 12))
            .orderByDesc { it.id }
            .toListResult()
    }

//    @MyLogLevel(LogLevelScopeEnum.ERROR)
//    @PostMapping("/last-log")
//    fun last_tasks(
//        @Require gitId: String,
//        @Require branch: String,
//        @Require appName: String,
//        lastStage: Boolean?,
//        request: HttpServletRequest
//    ): ListResult<JenkinsLogItemData> {
//        mor.dev.jenkinsLog.query()
//            .where { it.gitData.id mongoEquals gitId }
//            .where { it.buildInfo.gitBranch  mongoIn branch.split(",").map { it.trim() }.filter { it.HasValue } }
//            .where { it.buildInfo.appName mongoEquals appName }
//            .apply {
//                if (lastStage != null && lastStage) {
//                    this.select_array_from_last({ it.stages }, 1)
//                }
//            }
//            .orderByDesc { it.id }
//            .toEntity()
//            .apply {
//                if (this == null) {
//                    return ListResult.error("找不到Jenkins发版信息")
//                }
//                if (this.status == JenkinsJobStatusEnum.Success) {
//                    return ListResult.of(this.stages).withValue(true)
//                } else if (this.status == JenkinsJobStatusEnum.Failure) {
//                    return ListResult.of(this.stages).withValue(true)
//                }
//                return ListResult.of(this.stages)
//            }
//    }

}
